﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;
    using WMS6.Web.UserControls;

    public class ShipmentOrderRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15441;
        protected GridView gvShipmentOderRpt;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalOpenQty;
        protected Label lblTotalOpenQtyText;
        protected Label lblTotalOriginalQty;
        protected Label lblTotalOriginalQtyText;
        protected Label lblTotalShippedQty;
        protected Label lblTotalShippedQtyText;
        protected Label lblTotalWeight;
        protected Label lblTotalWeightText;
        protected Label lblTotalWgt;
        protected Label lblTotalWgtText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalOpenQty;
        private decimal TotalOriginalQty;
        private decimal TotalShippedQty;
        private decimal TotalWeight;
        private decimal TotalWgt;
        protected UpdatePanel up1;

        public void BindData2ShipmentOderRptList(ShipmentOrderRptQueryEntity shipmentOrderRptQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(shipmentOrderRptQuery.CurrentPage);
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetShipmentOrderRptInfo(shipmentOrderRptQuery);
            base.BindData2GridView(this.gvShipmentOderRpt, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            shipmentOrderRptQuery.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = shipmentOrderRptQuery.CurrentPage;
            shipmentOrderRptQuery.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetShipmentOrderRptInfo(shipmentOrderRptQuery))
                {
                    base.Export2Excel("shipmentOrderRpt.xls", "15441", this.gvShipmentOderRpt, ds);
                }
            }
            finally
            {
                shipmentOrderRptQuery.IsGetAll = false;
                shipmentOrderRptQuery.CurrentPage = currentPageIndex;
                this.SetShipmentOrderRptQuery(shipmentOrderRptQuery);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            shipmentOrderRptQuery.CurrentPage = "0";
            this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            shipmentOrderRptQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            shipmentOrderRptQuery.ORDER_ID = "";
            shipmentOrderRptQuery.EXTERN_ORDER_ID = "";
            shipmentOrderRptQuery.EXTERNAL_ORDER_ID2 = "";
            shipmentOrderRptQuery.OWNER_ID = "";
            shipmentOrderRptQuery.TYPE = "";
            shipmentOrderRptQuery.StartDateTime = "";
            shipmentOrderRptQuery.EndDateTime = "";
            shipmentOrderRptQuery.SKU_ID = "";
            shipmentOrderRptQuery.SKUCLASS_ID = "";
            shipmentOrderRptQuery.DESCR = "";
            shipmentOrderRptQuery.CONSIGNEE_ID = "";
            shipmentOrderRptQuery.LOT_ATTR01 = "";
            shipmentOrderRptQuery.LOT_ATTR02 = "";
            shipmentOrderRptQuery.LOT_ATTR03 = "";
            shipmentOrderRptQuery.LOT_ATTR04 = "";
            shipmentOrderRptQuery.LOT_ATTR05 = "";
            shipmentOrderRptQuery.LOT_ATTR06 = "";
            shipmentOrderRptQuery.LOT_ATTR07 = "";
            shipmentOrderRptQuery.LOT_ATTR08 = "";
            shipmentOrderRptQuery.LOT_ATTR09 = "";
            shipmentOrderRptQuery.LOT_ATTR10 = "";
            shipmentOrderRptQuery.EXTERNAL_LOT = "";
            this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            if (int.Parse(shipmentOrderRptQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                shipmentOrderRptQuery.CurrentPage = (int.Parse(shipmentOrderRptQuery.CurrentPage) + 1).ToString();
                this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            if (int.Parse(shipmentOrderRptQuery.CurrentPage) > 0)
            {
                shipmentOrderRptQuery.CurrentPage = (int.Parse(shipmentOrderRptQuery.CurrentPage) - 1).ToString();
                this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
            this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
        }

        private ShipmentOrderRptQueryEntity GetShipmentOrderRptQueryEntity()
        {
            return (base.PageQueryEntitySession as ShipmentOrderRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
                shipmentOrderRptQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
            }
        }

        protected void gvShipmentOderRpt_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15441", this.gvShipmentOderRpt);
        }

        protected void gvShipmentOderRpt_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblOriginalQty")) != null)
                {
                    this.TotalOriginalQty += decimal.Parse((((Label) e.Row.FindControl("lblOriginalQty")).Text != "") ? ((Label) e.Row.FindControl("lblOriginalQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblShippedQty")) != null)
                {
                    this.TotalShippedQty += decimal.Parse((((Label) e.Row.FindControl("lblShippedQty")).Text != "") ? ((Label) e.Row.FindControl("lblShippedQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblOpenQty")) != null)
                {
                    this.TotalOpenQty += decimal.Parse((((Label) e.Row.FindControl("lblOpenQty")).Text != "") ? ((Label) e.Row.FindControl("lblOpenQty")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalWgt")) != null)
                {
                    this.TotalWgt += decimal.Parse((((Label) e.Row.FindControl("lblTotalWgt")).Text != "") ? ((Label) e.Row.FindControl("lblTotalWgt")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalWeight")) != null)
                {
                    this.TotalWeight += decimal.Parse((((Label) e.Row.FindControl("lblTotalWeight")).Text != "") ? ((Label) e.Row.FindControl("lblTotalWeight")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblTotalCube")) != null)
                {
                    this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblTotalCube")).Text != "") ? ((Label) e.Row.FindControl("lblTotalCube")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalOriginalQtyText.Text = base.GetPreferenceDigit(this.TotalOriginalQty);
                this.lblTotalShippedQtyText.Text = base.GetPreferenceDigit(this.TotalShippedQty);
                this.lblTotalOpenQtyText.Text = base.GetPreferenceDigit(this.TotalOpenQty);
                this.lblTotalWgtText.Text = base.GetPreferenceDigit(this.TotalWgt);
                this.lblTotalWeightText.Text = base.GetPreferenceDigit(this.TotalWeight);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
            }
        }

        protected void gvShipmentOderRpt_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                ShipmentOrderRptQueryEntity shipmentOrderRptQuery = this.GetShipmentOrderRptQueryEntity();
                shipmentOrderRptQuery.SortField = e.SortExpression;
                if (shipmentOrderRptQuery.SortDirection == "ASC")
                {
                    shipmentOrderRptQuery.SortDirection = "DESC";
                }
                else if (shipmentOrderRptQuery.SortDirection == "DESC")
                {
                    shipmentOrderRptQuery.SortDirection = "ASC";
                }
                this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            ShipmentOrderRptQueryEntity shipmentOrderRptQuery = new ShipmentOrderRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    shipmentOrderRptQuery.ORDER_ID = this.PreviousPage.OrderID;
                    shipmentOrderRptQuery.EXTERN_ORDER_ID = this.PreviousPage.ExternOrderID;
                    shipmentOrderRptQuery.EXTERNAL_ORDER_ID2 = this.PreviousPage.ExternOrderID;
                    shipmentOrderRptQuery.OWNER_ID = this.PreviousPage.OwnerID;
                    shipmentOrderRptQuery.TYPE = this.PreviousPage.Type;
                    shipmentOrderRptQuery.StartDateTime = this.PreviousPage.StartTime;
                    shipmentOrderRptQuery.EndDateTime = this.PreviousPage.EndTime;
                    shipmentOrderRptQuery.SKU_ID = this.PreviousPage.SkuID;
                    shipmentOrderRptQuery.SKUCLASS_ID = this.PreviousPage.SkuClassID;
                    shipmentOrderRptQuery.CONSIGNEE_ID = this.PreviousPage.ConsigneeID;
                    shipmentOrderRptQuery.DESCR = this.PreviousPage.Descr;
                    shipmentOrderRptQuery.LOT_ATTR01 = this.PreviousPage.LotAttribute01;
                    shipmentOrderRptQuery.LOT_ATTR02 = this.PreviousPage.LotAttribute02;
                    shipmentOrderRptQuery.LOT_ATTR03 = this.PreviousPage.LotAttribute03;
                    shipmentOrderRptQuery.LOT_ATTR04 = this.PreviousPage.LotAttribute04;
                    shipmentOrderRptQuery.LOT_ATTR05 = this.PreviousPage.LotAttribute05;
                    shipmentOrderRptQuery.LOT_ATTR06 = this.PreviousPage.LotAttribute06;
                    shipmentOrderRptQuery.LOT_ATTR07 = this.PreviousPage.LotAttribute07;
                    shipmentOrderRptQuery.LOT_ATTR08 = this.PreviousPage.LotAttribute08;
                    shipmentOrderRptQuery.LOT_ATTR09 = this.PreviousPage.LotAttribute09;
                    shipmentOrderRptQuery.LOT_ATTR10 = this.PreviousPage.LotAttribute10;
                    shipmentOrderRptQuery.EXTERNAL_LOT = this.PreviousPage.ExternalLot;
                }
                else
                {
                    shipmentOrderRptQuery.ORDER_ID = "";
                    shipmentOrderRptQuery.EXTERN_ORDER_ID = "";
                    shipmentOrderRptQuery.EXTERNAL_ORDER_ID2 = "";
                    shipmentOrderRptQuery.OWNER_ID = "";
                    shipmentOrderRptQuery.TYPE = "";
                    shipmentOrderRptQuery.StartDateTime = "";
                    shipmentOrderRptQuery.EndDateTime = "";
                    shipmentOrderRptQuery.SKU_ID = "";
                    shipmentOrderRptQuery.SKUCLASS_ID = "";
                    shipmentOrderRptQuery.DESCR = "";
                    shipmentOrderRptQuery.CONSIGNEE_ID = "";
                    shipmentOrderRptQuery.LOT_ATTR01 = "";
                    shipmentOrderRptQuery.LOT_ATTR02 = "";
                    shipmentOrderRptQuery.LOT_ATTR03 = "";
                    shipmentOrderRptQuery.LOT_ATTR04 = "";
                    shipmentOrderRptQuery.LOT_ATTR05 = "";
                    shipmentOrderRptQuery.LOT_ATTR06 = "";
                    shipmentOrderRptQuery.LOT_ATTR07 = "";
                    shipmentOrderRptQuery.LOT_ATTR08 = "";
                    shipmentOrderRptQuery.LOT_ATTR09 = "";
                    shipmentOrderRptQuery.LOT_ATTR10 = "";
                    shipmentOrderRptQuery.EXTERNAL_LOT = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            shipmentOrderRptQuery.IsGetAll = false;
            shipmentOrderRptQuery.CurrentPage = "0";
            shipmentOrderRptQuery.PageSize = this.gvShipmentOderRpt.PageSize.ToString();
            shipmentOrderRptQuery.SortField = "ORDER_ID DESC,LINE_ID";
            shipmentOrderRptQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvShipmentOderRpt.PageSize;
            this.SetShipmentOrderRptQuery(shipmentOrderRptQuery);
            this.BindData2ShipmentOderRptList(shipmentOrderRptQuery);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvShipmentOderRpt);
                this.LoadData();
            }
        }

        public void SetShipmentOrderRptQuery(ShipmentOrderRptQueryEntity shipmentOrderRptQuery)
        {
            base.PageQueryEntitySession = shipmentOrderRptQuery;
        }

        public ShipmentOrderRptQuery PreviousPage
        {
            get
            {
                return (ShipmentOrderRptQuery) base.PreviousPage;
            }
        }
    }
}

